package com.bobo.spring;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到参数 name : " + name;
    }
    @RequestMapping("/r2")
    // 以Integer包装类为参数类型时,不传参时允许默认为null
    // 如果以int基础类型为参数类型,不允许为null,不允许不传参
    public String r2(Integer age){
        return "接收到参数 age : " + age;
    }
    @RequestMapping(value = "/r3",method = RequestMethod.GET)
    public String r3(String id){
        return "接收到参数 id : " + id;
    }
    // 发送请求:
    // http://127.0.0.1:8080/request/r5?name=1551&password=1515&age=1515
    @RequestMapping("/r4")
    public String r4(String name,String password,Integer age){
        return "name : " + name + ",password : " + password + ",age : " + age;
    }
    // 发送请求:(和r4相同 -> Spring会检查Person类的属性,然后自动将URL中同名参数注入对象属性中)
    // http://127.0.0.1:8080/request/r5?name=1551&password=1515&age=1515
    // 如果Person中的age为int,那么
    // 调用r5不传参,则Person为{null,null,0}  (不像直接以int为参数会直接失败)
    @RequestMapping("/r5")
    public String r5(Person p){
        return "name : " + p.getName() + ",password : " + p.getPassword()
                + ",age : " + p.getAge();
    }
    // @RequestParam是"用于请求的查询参数中,提取值的注解"
    // (此处表示:从请求的查询参数中查询名为sa的参数,并将其值赋给re)
    // (此时传re会被忽略,因为只在意sa)(对r6的http请求"true = 必传参数")
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "sa", required = true) String re){
        return "re : " + re;
    }
    // http请求:
    // http://127.0.0.1:8080/request/r7?arr=zhangsan&arr=lisi&arr=wangwu
    // return:[zhangsan, lisi, wangwu]
    @RequestMapping("/r7")
    public String r7(String[] arr){
        return Arrays.toString(arr);
    }
    // http请求:(同上一个)
    // http://127.0.0.1:8080/request/r8?list=zhangsan&list=lisi&list=wangwu
    // (List是接口,Spring无法直接实例化,所以需要@RequestParam注解
    // 告诉Spring:请将请求中的list参数值收集起来,并封装成一个List对象)
    @RequestMapping("/r8")
    public String r8(@RequestParam List<String> list){
        Object[] arr = list.toArray();
        return Arrays.toString(arr);
    }
    // 标记成"找不到list也不报错,而是将list初始化为(空列表)",
    @RequestMapping("/r81")
    public String r81(@RequestParam(required = false,defaultValue = "") ArrayList<String> list){
        if(list.isEmpty()){
            return null;
        }
        Object[] arr = list.toArray();
        return arr.toString();
    }
    // @RequestBody:从请求的正文(Body)中解析数据(JSON)并映射到p
    // *** Spring MVC 不允许在同一个方法中使用多个 @RequestBody 参数 ***
    @RequestMapping("/r9")
    public String r9(@RequestBody Person p){
        return "name : " + p.getName() + ",password : " + p.getPassword()
                + ",age : " + p.getAge();
    }
    // 错误的!此时会将整个body作为字符串绑定到name,而age无法接收到值!
    // 返回:
    // 接收到参数name: {
    //    "name":"zhangsan",
    //    "age":15
    //},age null
    @RequestMapping("/r91")
    public String r91(@RequestBody String name,Integer age){
        return "接收到参数name: " + name + ",age " + age;
    }
    // 正确的.将参数包装成一个类
    @RequestMapping("/r92")
    public String r92(@RequestBody Person person){
        return "name:" + person.getName() + ",age:" + person.getAge()
                + ",password:" + person.getPassword();
    }
    // {}中为一个可变值,它从http请求的url中获取资源作为其值
    // (但方法体规定该变值为Integer类型,则不能传其他类型值)
    @RequestMapping("/r10/{articleId}")
    public String r10(@PathVariable Integer articleId){
        return "articleId : " + articleId;
    }
    @RequestMapping("/r11/{articleId}/{articleName}")
    public String r11(@PathVariable Integer articleId,@PathVariable String articleName){
        return "articleId : " + articleId + ",articleName : " + articleName;
    }
    // MultipartFile代表"文件上传对象",可以向r12上传文件
    // 如:上传一个图片, 日志输出:file /n image/png
    @RequestMapping("/r12")
    public String r12(MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getContentType());
        return "接收到文件 file";
    }
    // 创建一个新文件,用来将"发送来的文件"保存到D盘的temp下.
    @RequestMapping("/r13")
    public String r13(MultipartFile file) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getContentType());
        File file1 = new File("D:/temp/" + file.getOriginalFilename());
        file.transferTo(file1);
        return "文件发送成功";
    }
}






